﻿using System;
using System.Text;
using System.Web.Mvc;
using TavDom.Library.Entities.ComplexTypes;

namespace TavDom.Library.UI.MVC.Helpers
{
    public static class PagingHelpers
    {
        public static MvcHtmlString Pagination(this HtmlHelper html, bool isSearch, Paging paging)
        {
            var stringBuilder = new StringBuilder();
            var totalPager = Convert.ToInt32((Math.Ceiling((decimal)paging.TotalItem / paging.ItemsPerPage)));
            for (var i = 1; i <= totalPager; i++)
            {
                var tagBuilder = new TagBuilder("li");

                if (paging.CurrentPage == i)
                {
                    tagBuilder.AddCssClass("active");
                }

                tagBuilder.InnerHtml = isSearch ? string.Format("<a class=\"searchPagingItem\" href=\"#\">{0}</a>", i) : string.Format("<a href=\"?page={0}\">{1}</a>", i, i);
                stringBuilder.Append(tagBuilder);
            }
            return MvcHtmlString.Create(stringBuilder.ToString());
        }
    }
}